package cn.shopex.multiimagepreview.app;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.shopex.multiimagepreview.R;

/**
 * Created by p on 2016/11/11.
 */
public class GalleryActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {
    private static final int SYSTEM_UI_BASE_VISIBILITY = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;

    private static final int SYSTEM_UI_IMMERSIVE = View.SYSTEM_UI_FLAG_IMMERSIVE
            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_FULLSCREEN;
    private Toolbar toolbar;
    private TextView textView;
    private ViewPager viewPager;
    private PagerAdapter mPagerAdapter;

    private int size;
    private List<GiftBean> mGiftList;
    private int position=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //初始化主题
        setTheme(R.style.BrowseThemeBase);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_browse_picture);

        mGiftList = (List<GiftBean>) getIntent().getSerializableExtra("Extra_List");
        position = getIntent().getIntExtra("position",0);
        initfindViewById();
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        ActionBar bar = getSupportActionBar();
        if (bar != null) {
            bar.setDisplayHomeAsUpEnabled(true); //显示返回箭头
        }


        mPagerAdapter = new PagerAdapter();
        viewPager.setAdapter(mPagerAdapter);
//        mViewPager.setPageTransformer(true, new ZoomOutPageTransformer());
        viewPager.setPageTransformer(true, new DepthPageTransformer());
        viewPager.setOffscreenPageLimit(1);
        viewPager.setCurrentItem(position);
        viewPager.addOnPageChangeListener(this);
        mPagerAdapter.notifyDataSetChanged();

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    /**初始化控件*/
    private void initfindViewById() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        textView = (TextView) findViewById(R.id.progress_txt_page);
        viewPager = (ViewPager) findViewById(R.id.pager);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        size = mGiftList.size();
        textView.setText(getResources().getString(R.string.meizi_limit_page,
                position + 1, size));
    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {
        if (state == ViewPager.SCROLL_STATE_DRAGGING) {
            hideSystemUi();
        }
    }


    private class PagerAdapter extends FragmentStatePagerAdapter {

        public PagerAdapter() {
            super(getSupportFragmentManager());
        }

        @Override
        public int getCount() {
            return mGiftList.size();
        }

        @Override
        public Fragment getItem(int position) {
            return GalleryFragment.newInstance(mGiftList.get(position).getImgUrl());
        }
    }

    private void hideSystemUi() {
        viewPager.setSystemUiVisibility(SYSTEM_UI_BASE_VISIBILITY | SYSTEM_UI_IMMERSIVE);
        toolbar.animate()
                .translationY(-toolbar.getHeight())
                .setDuration(400)
                .setInterpolator(new DecelerateInterpolator(2))
                .start();
    }

    private void showSystemUi() {
        viewPager.setSystemUiVisibility(SYSTEM_UI_BASE_VISIBILITY);
        toolbar.animate()
                .translationY(0)
                .setDuration(400)
                .setInterpolator(new DecelerateInterpolator(2))
                .start();
    }


    public void switchToolbar() {
        if (toolbar.getTranslationY() == 0) {
            hideSystemUi();
        } else {
            showSystemUi();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        overridePendingTransition(R.anim.alpha_in, R.anim.alpha_out);
    }

    @Override
    public void onBackPressed() {
        int count = getSupportFragmentManager().getBackStackEntryCount();
        if (count == 0) {
            super.onBackPressed();
        } else {
            getSupportFragmentManager().popBackStack();
        }
    }
}
